package cn.homework.web.servlet;

import cn.homework.domain.Product;
import cn.homework.service.CommodityService;
import cn.homework.service.impl.CommodityServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author ACT
 */
@WebServlet("/commodityServlet")
public class CommodityServlet extends HttpServlet {
    private CommodityService service = new CommodityServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String demand = request.getParameter("demand");
        switch (demand) {
            case "show": {
                commodityShow(request, response);
                break;
            }
            case "add": {
                commodityAdd(request, response);
                break;
            }
            case "delete": {
                commodityDelete(request, response);
                break;
            }
            case "find": {
                commodityFind(request, response);
                break;
            }
            case "update": {
                commodityUpdate(request, response);
                break;
            }
            case "query": {
                commodityQueryBuCondition(request, response);
                break;
            }
            default:
                break;
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    /**
     * 商品的展示
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void commodityShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Product> commodityShowAllList = service.findAllCommodity();
        HttpSession session = request.getSession();
        session.setAttribute("commodityShowAllList", commodityShowAllList);
        response.sendRedirect(request.getContextPath() + "/BackstageCommodityShow.jsp");
    }

    /**
     * 商品的添加
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void commodityAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        Product product = new Product();
        try {
            BeanUtils.populate(product, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        service.addCommodity(product);
        commodityShow(request, response);
    }

    /**
     * 商品的删除
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void commodityDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        service.delCommodityById(id);
        commodityShow(request, response);
    }

    /**
     * 根据商品id查询商品
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void commodityFind(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Product product = service.findCommodityById(id);
        HttpSession session = request.getSession();
        session.setAttribute("commodity", product);
        response.sendRedirect(request.getContextPath() + "/BackstageCommodityUpdate.jsp");
    }

    /**
     * 商品信息的修改
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void commodityUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Map<String, String[]> map = request.getParameterMap();
        Product product = new Product();
        try {
            BeanUtils.populate(product, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        service.updateCommodity(id, product);
        commodityShow(request, response);
    }

    /**
     * 根据条件查询商品信息
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void commodityQueryBuCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        List<Product> commodityList = service.findCommodityCondition(map);
        HttpSession session = request.getSession();
        session.setAttribute("commodityList", commodityList);
        response.sendRedirect(request.getContextPath() + "/BackstageCommodityQuery.jsp");
    }
}
